/**
 * Created by DELL on 2017/3/22.
 */
var agentId=common.getQueryString("agentId");
var canEdit=common.getQueryString("edit");
var $form=$("form");
var dataPhotos=$form.find("#data-photo-list");
var authRadio=$form.find(".data-isAuth");
var noPassCause=$form.find(".noPassCause");
var noPassCauseTextArea=noPassCause.find("textarea");
$form.validate({
    /**
     * ignore：忽略某些元素不验证
     * @type {String}
     * 例如：ignore: ".ignore" ，忽略所有的带ignore class 的标签
     */
    ignore: ".ignore"
});
window.formObject = $form;
var regImg=/(\.jpg|\.jpeg|\.gif|\.png)$/;
var $agentHeadPhoto=$("#agent-head-photo");
var $groupPhotoList=$("#group-photo-list");
var $delTvInfoBtn=$(".del-panel-info");
$.post(window.globalHost+common.agentInfoPort,{id:agentId}).success(function (data) {
    var record=common.transformData(data);
    common.initDivById(record);
    common.initInputByName({
        id:record.id,
        noPassCause:record.noPassCause
    });
    var TVPanelInfo=record.perfect;
    if(!!record.qualified && record.qualified!=0){
        $delTvInfoBtn.show();
        common.initDivById({
            personalEnounce:TVPanelInfo.personalEnounce,
            workLife:TVPanelInfo.workLife+"年",
            visitHouses:TVPanelInfo.visitHouses,
            serviceCustomers:TVPanelInfo.serviceCustomers,
            dealCustomers:TVPanelInfo.dealCustomers
        });
        var headImage=regImg.test(TVPanelInfo.headImage)?'<img src="'+window.fileHost+TVPanelInfo.headImage+'" />':'无图片';
        $agentHeadPhoto.html(headImage);
        var groupPhotoList=!!TVPanelInfo.groupPhoto?TVPanelInfo.groupPhoto.split(","):[];
        var groupPhotos=groupPhotoList.map(function (ele, idx) {
            return regImg.test(ele)?'<li><img src="'+window.fileHost+ele+'" alt=""></li>':'';
        });
        $groupPhotoList.html(groupPhotos);
    }else{
        $delTvInfoBtn.remove();
    }

    initImageByClass(record,"frontPhoto");
    initImageByClass(record,"backPhoto");
    initImageByClass(record,"businessCard");
    renderRadio(authRadio,record['isAuth'],record['noPassCause']);

});
    function initImageByClass(json,key) {
        $("."+key).attr("src",window.fileHost+json[key]);
    }

renderRadio=function (selector,checkedValue,noPassCauseValue) {
    var radios=$(selector).find("input[type='radio']");
    checkedValue==="2"?noPassCause.show() && noPassCauseTextArea.attr("required","true").val(noPassCauseValue):noPassCause.hide() && noPassCauseTextArea.removeAttr("required").val("");
    $.each(radios,function (idx,ele) {
        if(ele.value===checkedValue){
            ele.checked=true;
        }
    });
    if(!canEdit){
        radios.attr("disabled","true");
        noPassCauseTextArea.attr("disabled","true").css({"backgroundColor":"#fff"});
    }
    authRadio.off("click","label").on("click","label",function () {
        if(!!canEdit){
            var $this=$(this);
            var selfAuth=$this.attr("for");
            selfAuth==='refuse'?noPassCause.show() && noPassCauseTextArea.attr("required","true").val(noPassCauseValue):noPassCause.hide() && noPassCauseTextArea.removeAttr("required").val("");
        }
    });
};

dataPhotos.off("click","li").on("click","li",function (ev) {
    var $this=$(this);
    var selfSrc=$this.find("img").attr("src");
    createImageOnDialog(selfSrc);
});
$groupPhotoList.off("click","li").on("click","li",function (ev) {
    var $this=$(this);
    var selfSrc=$this.find("img").attr("src");
    createImageOnDialog(selfSrc);
});
$agentHeadPhoto.off("click").on("click",function (ev) {
    var $this=$(this);
    var selfSrc=$this.find("img").attr("src");
    createImageOnDialog(selfSrc);
    ev.stopPropagation();
});

function createImageOnDialog (src) {
    var content='<p style="width:600px;margin:0;"><img style="width:100%;" src="'+src+'" alt=""></p>';
    layer.open({
        type:1,
        title:false,
        shadeClose:true,
        content:content,
        area:['600px']
    });
}
var delCause='<div class="form-group">' +
    '<label class="col-sm-3 control-label">请输入理由：</label>' +
    '<div class="col-sm-9" style="margin-top: 10px;"><textarea name="remark" style="height: 160px;"></textarea> </div>' +
    '</div>';
$delTvInfoBtn.off("click").on("click",function (e) {
    common.confirm({msg:"确认删除该用户的看板资料？"}).then(function () {
        var sendData={id:agentId,qualified:0};
        common.openTempDialog({
            type: 1,
            title: '删除看板资料原因',
            area: ['600px', '300px'],
            content: delCause,
            FailedMsg: "请输入删除原因",
            sendData: sendData,
            ajaxUrl: window.globalHost+common.delTVPanelInfoPort,
            contentOnLoad: function (layerDocument) {

            },
            beforeSend: function (layerDocument) {
                sendData.remark=layerDocument.find("textarea[name='remark']").val().trim().replace(/\r\n/g, "\\r\\n");
                return sendData.remark;
            },
            resOk: function () {
                //如果更新成功，刷新页面
                window.location.reload();
            }
        });
    });

});